<?php

namespace app\common\model\system;

use app\common\model\BaseModel;
use think\facade\Db;

class Log extends BaseModel
{
    protected $table;

    public function __construct(array $data = [])
    {
        $this->table = 'tp_system_log' . date('Ym');
        $this->checkTable();
        parent::__construct($data);
    }

    private function checkTable()
    {
        if (!$this->tableExists($this->table)) {
              $sql = "CREATE TABLE `{$this->table}` (
             `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
              `admin_id` int(10) unsigned DEFAULT '0' COMMENT '管理员ID',
              `url` varchar(1500) NOT NULL DEFAULT '' COMMENT '操作页面',
              `method` varchar(50) NOT NULL COMMENT '请求方法',
              `title` varchar(100) DEFAULT '' COMMENT '日志标题',
              `content` text NOT NULL COMMENT '内容',
              `ip` varchar(50) NOT NULL DEFAULT '' COMMENT 'IP',
              `useragent` varchar(255) DEFAULT '' COMMENT 'User-Agent',
              `create_time` int(10) DEFAULT NULL COMMENT '操作时间',
              PRIMARY KEY (`id`)
            ) ENGINE=InnoDB AUTO_INCREMENT=630 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='后台操作日志表'";
            Db::query($sql);
        }
    }
}